//
//  ChooseView.m
//  LiuHeBao
//
//  Created by zr on 2018/8/24.
//  Copyright © 2018年 zr. All rights reserved.
//

#import "ChooseView.h"

@interface ChooseView ()<UIPickerViewDelegate,UIPickerViewDataSource>

@property (nonatomic, weak)UIView * topView;
@property (nonatomic, weak)UIView * bottomView;
@property (nonatomic, weak)UIView * middleView;
@property (nonatomic, strong)UIButton * cancleButton;
@property (nonatomic, strong)UIButton * sureButton;
@property (nonatomic, weak)UIPickerView * pickerView;
@property (nonatomic, strong)NSArray * xinzuoArray;
@property (nonatomic, strong)NSArray * shengArray;
@property (nonatomic, strong)NSArray * styleArray;
@property (nonatomic, copy)NSString * selectStr;
@property (nonatomic, assign)NSInteger row;

@end

@implementation ChooseView

- (instancetype)initWithFrame:(CGRect)frame{
    
    if ([super initWithFrame:frame]) {
        
        [self setupUI];
    }
    
    return self;
}


- (void)setupUI{
    
//    self.backgroundColor = WHITE_COLOR;
    self.userInteractionEnabled = YES;
    self.row = 0;
    
    self.chooseType = @"xinzuo";
    
    self.xinzuoArray = @[@"白羊座",@"金牛座",@"双子座",@"巨蟹座",@"狮子座",@"处女座",@"天秤座",@"天蝎座",@"射手座",@"摩羯座",@"水瓶座",@"双鱼座"];
    self.shengArray = @[@"鼠",@"牛",@"虎",@"兔",@"龙",@"蛇",@"马",@"羊",@"猴",@"鸡",@"狗",@"猪"];
    self.styleArray = @[@"A",@"B",@"O",@"AB"];
    
    
    UIView * topView = [UIView new];

    topView.backgroundColor = BLACK_COLOR;
    topView.alpha = 0.4;

    self.topView = topView;

    [self addSubview:self.topView];


    UIView * middleView = [UIView new];

    middleView.backgroundColor = ColorWithRGB(0xe6e6e6);
    middleView.userInteractionEnabled = YES;

    self.middleView = middleView;

    [self addSubview:self.middleView];


    self.cancleButton = [self setupButtonWithTitle:@"取消"];
    [self.cancleButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [self.middleView addSubview:self.cancleButton];

    self.sureButton = [self setupButtonWithTitle:@"确定"];
    [self.sureButton setTitleColor:MAINCOLOR forState:UIControlStateNormal];
    [self.middleView addSubview:self.sureButton];
    
    // 选择框
    UIPickerView *pickerView = [[UIPickerView alloc] init];
    
    // 显示选中框
    pickerView.showsSelectionIndicator=YES;
    pickerView.dataSource = self;
    pickerView.delegate = self;
    pickerView.backgroundColor = WHITE_COLOR;
    
    self.pickerView = pickerView;
    
    [self addSubview:self.pickerView];
}


- (void)layoutSubviews{
    
    [self.pickerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.bottom.mas_equalTo(self);
        make.height.mas_equalTo(230 * SCREENH6);
    }];
    
    [self.middleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.pickerView.mas_top);
        make.leading.trailing.mas_equalTo(self);
        make.height.mas_equalTo(45 * SCREENH6);
    }];
    
    [self.cancleButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.middleView).offset(10 * SCREENH6);
        make.centerY.mas_equalTo(self.middleView);
    }];
    
    [self.sureButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.middleView).offset(-10 * SCREENH6);
        make.centerY.mas_equalTo(self.middleView);
    }];
    
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.trailing.mas_equalTo(self);
        make.bottom.mas_equalTo(self.middleView.mas_top);
    }];
}


// pickerView 列数

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    
    return 1;
    
}

// pickerView 每列个数

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    
    if ([self.chooseType isEqualToString:@"xinzuo"]) {
        
        return self.xinzuoArray.count;
    }else if ([self.chooseType isEqualToString:@"sheng"]){
        
        return self.shengArray.count;
    }else{
        
        return self.styleArray.count;
    }
    
}

// 返回每行的标题
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    
    if ([self.chooseType isEqualToString:@"xinzuo"]) {
        
        return self.xinzuoArray[row];
    }else if ([self.chooseType isEqualToString:@"sheng"]){
        
        return self.shengArray[row];
    }else{
        
        return self.styleArray[row];
    }
}


// 选中行显示在label上
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    
    self.row = row;
}


- (UIButton *)setupButtonWithTitle:(NSString *)title{
    
    UIButton * button = [UIButton new];
    
    [button setTitle:title forState:UIControlStateNormal];
    [button addTarget:self action:@selector(cilckMiddleButton:) forControlEvents:UIControlEventTouchUpInside];
    button.titleLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    
    return button;
}


- (void)cilckMiddleButton:(UIButton *)button{
    
    if ([self.chooseType isEqualToString:@"xinzuo"]) {
        
        self.selectStr = self.xinzuoArray[self.row];
        
    }else if ([self.chooseType isEqualToString:@"sheng"]){
        
        self.selectStr = self.shengArray[self.row];
    }else{
        
        self.selectStr = self.styleArray[self.row];
    }
    
    if (button == self.cancleButton) {
    
        NSDictionary * dict = @{@"type":@"cancle"};
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"clickSelectButton" object:nil userInfo:dict];
    }else{
        
        NSDictionary * dict = @{@"type":@"sure",@"value":self.selectStr};
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"clickSelectButton" object:nil userInfo:dict];
    }
    
}

@end
